#include <iostream>
#include <queue>
using namespace std;


struct line{
	int num,index;
};


int cac(int n,int *a){
	int sum=0;
	int ans=0;
	for(int i=0;i<n;i++)
		sum += a[i];
	sum /= n;
	for(int i=0;i<n;i++){
		ans += (a[i]-sum)*(a[i]-sum);
	}
	return ans;
}

bool cmp(line a,line b){
	return a.num<b.num;
}


int main(){
	int k=0;
	queue<int> q;
	int n,m;
	cin>>m>>n;
	line a[n];
	for(int i=0;i<n;i++){
		a[i].index = i+1;
		a[i].num = 0;
	}
	for(int i=0;i<n;i++){
		int b[m];
		for(int j=0;j<m;j++)
			cin>>b[j];
		a[i].num += cac(m,b);
	}
	for(int i=0;i<n;i++){
		int b[m];
		for(int j=0;j<m;j++)
			cin>>b[j];
		a[i].num += cac(m,b);
	}
//	for(int i=0;i<n;i++)
//		cout<<a[i].num<<" ";
//	cout<<endl;
	for(int i=0;i<n;i++){
		for(int j=0;j<n-i-1;j++){
			if(a[j].num >= a[j+1].num){
				k++;
//				cout<<j<<" "<<j+1<<endl;
				q.push(j);
				q.push(j+1);
				line l=a[j];
				a[j] = a[j+1];
				a[j+1] = l;
			}
//			else{
//				cout<<"1:"<<j<<" "<<j+1<<" "<<a[j].num<<" "<<a[j+1].num<<endl;
//			}
		}
	}
	cout<<k<<endl;
	while(!q.empty()){
		cout<<q.front()+1<<" ";
		q.pop();
		cout<<q.front()+1<<endl;
		q.pop();
	}
	return 0;
}
